push([[
null = "NULL"

function table.size(tab)
    local length = 0
    for _ in pairs(tab) do length = length + 1 end
    return length
end

ActiveRecord = Class:Create()
ActiveRecord.sql = nil

function ActiveRecord:Initialize(handler)
	t = Class:Create(ActiveRecord)
	t.sql = handler
	return t;
end

function ActiveRecord:LoadTable(name)
	t = Class:Create(ActiveRecord_Table)
	t.sql = self.sql
	t.name = name
	return t;
end

ActiveRecord_Table = Class:Create()
ActiveRecord_Table.sql = nil
ActiveRecord_Table.name = nil

function ActiveRecord_Table:RowExists(case)
	local qtext = "SELECT * FROM " .. self.name .. " WHERE "
	
	i = 1
	for k,v in pairs(case) do
		if i == table.size(case) then
			qtext = qtext .. k .. " = '" .. v .. "'"
		else
			qtext = qtext .. k .. " = '" .. v .. "', "
		end
		
		i = i + 1
	end
	
	--outputDebugString("Executing query generated by AR:RowExists: " .. qtext)
	
	Query = self.sql:Query(qtext)
	
	if Query:numRows() > 0 then
		return true;
	end
	
	return false;
end

function ActiveRecord_Table:Select(case)
	if case then
		local qtext = "SELECT * FROM " .. self.name .. " WHERE "
		
		i = 1
		for k,v in pairs(case) do
			if i == table.size(case) then
				qtext = qtext .. k .. " = '" .. v .. "'"
			else
				qtext = qtext .. k .. " = '" .. v .. "' AND "
			end
			
			i = i + 1
		end
		
		--outputDebugString("Executing query generated by AR:Select: " .. qtext)
		
		Query = self.sql:Query(qtext)
		
		return Query;
	else
		Query = self.sql:Query("SELECT * FROM " .. self.name)
		
		return Query;
	end
end

function ActiveRecord_Table:Insert(case)
	local qtext = "INSERT INTO " .. self.name .. " VALUES("
	
	i = 0
	for k,v in ipairs(case) do
		if k == #case then
			if v == null or type(v) == "number" then
				qtext = qtext .. "" .. v .. " )"
			else
				qtext = qtext .. "'" .. v .. "' )"
			end
		else
			if v == null or type(v) == "number" then
				qtext = qtext .. "" .. v .. ", "
			else
				qtext = qtext .. "'" .. v .. "', "
			end
		end
		
		i = i + 1
	end
	
	--outputDebugString("Executing query generated by AR:Insert: " .. qtext)
	
	Query = self.sql:Query(qtext)
	
	return Query;
end

function ActiveRecord_Table:Delete(case)
	if case then
		local qtext = "DELETE FROM " .. self.name .. " WHERE "
		
		i = 1
		for k,v in pairs(case) do
			if i == table.size(case) then
				qtext = qtext .. k .. " = '" .. v .. "'"
			else
				qtext = qtext .. k .. " = '" .. v .. "' AND "
			end
			
			i = i + 1
		end
		
		--outputDebugString("Executing query generated by AR:Delete: " .. qtext)
		
		Query = self.sql:Query(qtext)
		
		return Query;
	else
		local qtext = "REMOVE FROM " .. self.name
		Query = self.sql:Query(qtext)
		
		--outputDebugString("Executing query generated by AR:Delete: " .. qtext)
		
		return Query;
	end
end

function ActiveRecord_Table:Update(case, where)
	local qtext = "UPDATE " .. self.name .. " SET "
	
	i = 0
	
	for k,v in ipairs(case) do
		if k == #case then
			qtext = qtext .. v .. " "
		else
			qtext = qtext .. v .. ", "
		end
		
		i = i + 1
	end
	
	qtext = qtext .. " WHERE "
		
	i = 1
	for k,v in pairs(where) do
		if i == table.size(where) then
			qtext = qtext .. k .. " = '" .. v .. "'"
		else
			qtext = qtext .. k .. " = '" .. v .. "' AND "
		end
		
		i = i + 1
	end
	
	--outputDebugString("Executing query generated by AR:Update: " .. qtext)
	
	Query = self.sql:Query(qtext)
	
	return Query;
end
]])